package com.shejimoshi.单例模式.单例模式的安全性.序列化攻击;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Singleton singleton = Singleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        oos.writeObject(singleton); // 序列化
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("singleton_file"));
        Singleton newSingleton = (Singleton) ois.readObject(); // 反序列化
        ois.close();

        System.out.println(singleton);
        System.out.println(newSingleton);
        System.out.println(singleton == newSingleton);

        System.out.println("《《《《《《特殊枚举序列化》》》》》》");
        Color color = Color.BLACK;
        ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("color_file"));
        oos1.writeObject(color); // 序列化
        oos1.close();

        ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("color_file"));
        Color newColor = (Color) ois1.readObject(); // 反序列化

        System.out.println(color);
        System.out.println(newColor);
        System.out.println(color == newColor);

    }
}
